﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace RAMED
{
    public partial class Bordereau_Form : Form
    {
        public List<Carte> Cartebordereau = new List<Carte>();

        public  LOT lot = new LOT();
        public Bordereau_Form()
        {
            InitializeComponent();
            RAMEDEntities1 context = SingletonContext.GetContext();
            comboboxannexebordereau.DataSource = context.Annexe_administratif;
            comboboxannexebordereau.DisplayMember = "nom_annexe_administratif";
            comboboxannexebordereau.ValueMember = "id_annexe_administratif";

           
        }

        private void dateTimePickerbor_ValueChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if( ValidateChildren(ValidationConstraints.Enabled))
            {

                 RAMEDEntities1 Context = SingletonContext.GetContext();
 	             Bordereau bordereauajoute = new Bordereau();
                 bordereauajoute.type_bordereau = txttypebordereau.Text;
                 bordereauajoute.Nombre_de_carte = utitlitaire.CasterString(txtnombrecarte.Text);
                 bordereauajoute.Annexe_administratif = ((Annexe_administratif)comboboxannexebordereau.SelectedItem);

                 foreach (Carte cart in Cartebordereau)
                 bordereauajoute.Cartes.Add(cart);
                 bordereauajoute.Retrait = false;
                 Context.SaveChanges();
                 lot.bordereauRecherche.Add(bordereauajoute);
                 lot.mettreAjouterFormGridViewlot();
                 
                 this.Dispose();
        }

        }

        private void txtannexebordereau_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void Bordereau_Form_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            RecherchebordereauForm bord = new RecherchebordereauForm();
            
            bord.Show();
        }

        private void txttypebordereau_Validating(object sender, CancelEventArgs e)
        {
            utitlitaire.ValiderNombre("Saisissez un type valide ", txttypebordereau, e, errorProvider1);

        }

        private void txtnombrecarte_Validating(object sender, CancelEventArgs e)
        {
            utitlitaire.ValiderNombre("Saisissez le nombre des cartes valide", txtnombrecarte, e, errorProvider1);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            CarteForm CF = new CarteForm();
            CF.bordereauform = this;
            CF.Show();
        }
        public void mettreAjourFormGridView()
        {
            dataGridViewbor.DataSource = null;
            remplirDataGridView();
            dataGridViewbor.Update();
            dataGridViewbor.Refresh();
        }

   
        private void remplirDataGridView()
        {
            dataGridViewbor.AutoGenerateColumns = false;
            dataGridViewbor.ColumnCount = 5;
            dataGridViewbor.Columns[0].HeaderText = "Nom";
            dataGridViewbor.Columns[0].DataPropertyName = "Nom_benefi";

            dataGridViewbor.Columns[1].HeaderText = "Prénom";
            dataGridViewbor.Columns[1].DataPropertyName = "Prenom_benefi";

            dataGridViewbor.Columns[2].HeaderText = "CIN";
            dataGridViewbor.Columns[2].DataPropertyName = "CIN_benefi";

            dataGridViewbor.Columns[3].HeaderText = "IPCM";
            dataGridViewbor.Columns[3].DataPropertyName = "IPCM";

            //dataGridViewbor.Columns[4].HeaderText = "Type";
            //dataGridViewbor.Columns[4].DataPropertyName = "type_beneficaire";

            dataGridViewbor.Columns[4].HeaderText = "Centre de santé";
            dataGridViewbor.Columns[4].DataPropertyName = "Centre_sante";

            dataGridViewbor.DataSource = Cartebordereau;
        }

        private void Button21_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
        }

        private void modifierButton_Click(object sender, EventArgs e)
        {
            if (dataGridViewbor.CurrentRow == null)
            {
                NotificationTest.NotificationManager nm = new NotificationTest.NotificationManager();
                nm.Show(this, "Veuillez sélectionner  un  Bénéficaire pour le modifier", Color.Red, 3000);
            }
            else
            {
                ModifierCatreAjoutBordereau mbf = new ModifierCatreAjoutBordereau();

                Carte carte = new Carte();
                carte = ((Carte)dataGridViewbor.CurrentRow.DataBoundItem);

                mbf.Carte = carte;
                mbf.InitialiserChamps();
                mbf.bordereauForm = this;
                mbf.Show();
            }
        }

        private void button1_Click_2(object sender, EventArgs e)
        {
            RecherchebordereauForm rbf = new RecherchebordereauForm();
            rbf.Show();
        }

        private void dataGridViewbor_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void txttypebordereau_TextChanged(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void txttypebordereau_Validating_1(object sender, CancelEventArgs e)
        {
            utitlitaire.ValiderChampsRecquit("le champs Type est obligatoire", txttypebordereau, e, errorProvider1);
        }

        private void txtnombrecarte_Validating_1(object sender, CancelEventArgs e)
        {
            utitlitaire.ValiderNombre("Saisez un nombre valide", txtnombrecarte, e, errorProvider1);
        }

      
    }
}
